package com.glela.api.sms.xinxi1;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloopen.rest.sdk.CCPRestSDK;
import com.glela.common.util.Config;
import com.glela.common.util.StringUtils;

/**
 * @author liujie
 *
 */
public class SendMsg {
	private static Logger logger = LoggerFactory.getLogger(SendMsg.class);
	//短信账号配置
	private static String sms_1xinxi_name;
	private static String sms_1xinxi_pwd;
	private static String sms_1xinxi_url;
	private static String sms_1xinxi_sign;
	//语音验证码账号配置
	private static String sms_yuntongxun_account_id;
	private static String sms_yuntongxun_auth_token;
	private static String sms_yuntongxun_server_ip;
	private static String sms_yuntongxun_port;
	private static String sms_yuntongxun_app_id;
	
	static{
		sms_1xinxi_name = Config.getString("sms.1xinxi.name");
		sms_1xinxi_pwd = Config.getString("sms.1xinxi.pwd");
		sms_1xinxi_url = Config.getString("sms.1xinxi.url");
		sms_1xinxi_sign = Config.getString("sms.1xinxi.sign");
		
		sms_yuntongxun_account_id = Config.getString("sms.yuntongxun.account_id");
		sms_yuntongxun_auth_token = Config.getString("sms.yuntongxun.auth_token");
		sms_yuntongxun_server_ip = Config.getString("sms.yuntongxun.server_ip");
		sms_yuntongxun_port = Config.getString("sms.yuntongxun.port");
		sms_yuntongxun_app_id = Config.getString("sms.yuntongxun.app_id");
	}
	
	/**
	 * 发送短信
	 * @param mobile
	 * @param msg
	 * @return 第三方发送编号
	 */
	public static String sendSms(String mobile,String msg) {
		if(StringUtils.isEmpty(mobile) || mobile.length()!=11 || StringUtils.isEmpty(msg)){
			return "";
		}
		try {
			StringBuilder arge = new StringBuilder(sms_1xinxi_url);
			arge.append("name=");
			arge.append(sms_1xinxi_name);
			arge.append("&pwd=");
			arge.append(sms_1xinxi_pwd);
			arge.append("&content=");
			arge.append(URLEncoder.encode(msg,"UTF-8"));
			arge.append("&mobile=");
			arge.append(mobile);
			arge.append("&sign=");
			arge.append(sms_1xinxi_sign);
			arge.append("&type=pt");

			URL url = new URL(arge.toString());
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestMethod("POST");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputline = in.readLine();
	        
	        logger.info("短信发送结果："+inputline);
	        if(StringUtils.isEmpty(inputline)){
	        	logger.error("短信发送结果为空！");
	        	return "";
	        }
	        String[] strs= inputline.split(",");
	        if(!strs[0].equals("0") || strs.length<2){
	        	logger.error("短信发送失败，返回值："+inputline);
	        }
	        return strs[1];
		} catch (Exception e) {
			logger.error("sms send exception", e);
		}
		return "";
	}
	
	public static String testSendSms(String mobile,String msg) {
		if(StringUtils.isEmpty(mobile) || mobile.length()!=11 || StringUtils.isEmpty(msg)){
			return "";
		}
		try {
			StringBuilder arge = new StringBuilder("http://sms.1xinxi.cn/asmx/smsservice.aspx?");
			arge.append("name=");
			arge.append("13489065@qq.com");
			arge.append("&pwd=");
			arge.append("F863C0172197E91577D758FE1335");
			arge.append("&content=");
			arge.append(URLEncoder.encode(msg,"UTF-8"));
			arge.append("&mobile=");
			arge.append(mobile);
			arge.append("&sign=");
			arge.append("");
			arge.append("&type=pt");

			URL url = new URL(arge.toString());
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestMethod("POST");
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        String inputline = in.readLine();
	        
	        logger.info("短信发送结果："+inputline);
	        if(StringUtils.isEmpty(inputline)){
	        	logger.error("短信发送结果为空！");
	        	return "";
	        }
	        String[] strs= inputline.split(",");
	        if(!strs[0].equals("0") || strs.length<2){
	        	logger.error("短信发送失败，返回值："+inputline);
	        }
	        return strs[1];
		} catch (Exception e) {
			logger.error("sms send exception", e);
		}
		return "";
	}

	
	/**
	 * 发送语音验证码
	 * @param mobile
	 * @param code
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String sendVoiceVerifyCode(String mobile,String code){
		if(StringUtils.isEmpty(mobile) || mobile.length()!=11 || StringUtils.isEmpty(code)){
			return "";
		}
		try{
			CCPRestSDK restAPI = new CCPRestSDK();
			restAPI.init(sms_yuntongxun_server_ip, sms_yuntongxun_port);
			restAPI.setAccount(sms_yuntongxun_account_id, sms_yuntongxun_auth_token);
			restAPI.setAppId(sms_yuntongxun_app_id);
			HashMap<String, Object> result = restAPI.voiceVerify(code, mobile,"","2","", "", "");
	
			if(result==null || result.get("statusCode")==null){
				logger.error("发送语音验证码异常，返回数据："+result);
				return "";
			}
			if(!"000000".equals(result.get("statusCode"))){
				logger.error("发送语音验证码失败[1]："+result);
				return "";
			}
			Map data = (HashMap)result.get("data");
			if(data==null || data.get("VoiceVerify")==null){
				logger.error("发送语音验证码失败[2]："+result);
				return "";
			}
			Map VoiceVerify = (HashMap)data.get("VoiceVerify");
			if(VoiceVerify==null || VoiceVerify.get("callSid")==null){
				logger.error("发送语音验证码失败[3]："+result);
				return "";
			}
			String callSid = (String)VoiceVerify.get("callSid");
			if(StringUtils.isEmpty(callSid)){
				logger.error("发送语音验证码失败[4]："+result);
				return "";
			}
			return callSid;
		}catch(Exception e){
			logger.error("发送语音验证码异常", e);
		}
		return "";
	}
	
	@SuppressWarnings("rawtypes")
	public static String testSendVoiceVerifyCode(String mobile,String code){
		if(StringUtils.isEmpty(mobile) || mobile.length()!=11 || StringUtils.isEmpty(code)){
			return "";
		}
		try{
			CCPRestSDK restAPI = new CCPRestSDK();
			restAPI.init("app.cloopen.com", "8883");
			restAPI.setAccount("8a216da8550b8ac00155143c1fb60b39", "d3b53b77216641559eadb85a5871f952");
			restAPI.setAppId("8a216da8550b8ac00155143c202a0b3f");
			HashMap<String, Object> result = restAPI.voiceVerify(code, mobile,"","2","", "", "");
	
			if(result==null || result.get("statusCode")==null){
				logger.error("发送语音验证码异常，返回数据："+result);
				return "";
			}
			if(!"000000".equals(result.get("statusCode"))){
				logger.error("发送语音验证码失败[1]："+result);
				return "";
			}
			Map data = (HashMap)result.get("data");
			if(data==null || data.get("VoiceVerify")==null){
				logger.error("发送语音验证码失败[2]："+result);
				return "";
			}
			Map VoiceVerify = (HashMap)data.get("VoiceVerify");
			if(VoiceVerify==null || VoiceVerify.get("callSid")==null){
				logger.error("发送语音验证码失败[3]："+result);
				return "";
			}
			String callSid = (String)VoiceVerify.get("callSid");
			if(StringUtils.isEmpty(callSid)){
				logger.error("发送语音验证码失败[4]："+result);
				return "";
			}
			return callSid;
		}catch(Exception e){
			logger.error("发送语音验证码异常", e);
		}
		return "";
	}
	
	public static void main(String args[]){
//		String f = SendMsg.testSendSms("13764322841","【test】您的验证码为：2563。（请在60秒内完成操作）");
//		System.out.println(">>>>>>>>>>>"+f);
		String ff = SendMsg.testSendVoiceVerifyCode("13598008451", "6352");
		System.out.println(">>>>>>>>>>>"+ff);
	}
}
